package com.oms.app.user.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oms.app.sequence.service.impl.SequenceManager;
import com.oms.app.user.model.Role;
import com.oms.app.user.service.IRoleService;
import com.oms.page.PageModel;
import com.oms.util.OMSFileUtils;
import com.oms.base.OMSConfigHolder;
/**
 * controller
 * @author wtj
 * @date Sep 10, 2012  10:08:42 PM
 */
@RequestMapping("")
public class RoleAction {
	private IRoleService roleService;
	private Map<String, Object> map = new HashMap<String, Object>();
	private static final String jspRootPaht = OMSConfigHolder.getWebRoot().replaceAll("\\\\", "/") +  "jsp";
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	/*** 后台管理 BEGIN ***/
	//进入用户管理列表
	@RequestMapping(value="role_roleManagerList", method=RequestMethod.GET)
	public String enterRoleManagerList(HttpServletRequest request){
		return "manager/role/roleManagerList";
	}
	 // 获取角色列表
	@ResponseBody
	@RequestMapping(value="/role_getList")
	public Map<String, Object> getRoleList(HttpServletRequest request){
		List list =  roleService.getRoleList(request);
		return PageModel.getListToString(list, request);
	}
	 // 获取角色列表
	@ResponseBody
	@RequestMapping(value="/role_getGroupOfRole/{groupId}")
	public Map<String, Object> role_getGroupOfRole(@PathVariable String groupId,HttpServletRequest request){
		map.clear();
		map.put("GROUPID", groupId);
		List list =  roleService.getGroupOfRole(map);
		return PageModel.getListToString(list, request);
	}
	 // 进入角色增加模块
	@RequestMapping(value="/role_addOrUpdateRole")
	public void enterAddOrUpdateRole(HttpServletRequest request,HttpServletResponse response){
		try {
			PrintWriter out = response.getWriter();
			out.print(OMSFileUtils.readFileToString(jspRootPaht+"/manager/role/addOrUpdateRole.jsp"));
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//return "manager/role/addOrUpdateRole";
	}
	 // 进入 设置角色与模块关联关系页面
	@RequestMapping(value="/role_roleSetupModule/{roleId}")
	public void  enterRoleSetupModule(@PathVariable String roleId,HttpServletRequest request,HttpServletResponse response){
		try {
			PrintWriter out = response.getWriter();
			out.print(OMSFileUtils.readFileToString(jspRootPaht+"/manager/role/roleSetupModule.jsp").replaceAll("\\{roleId\\}", roleId));
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//return "manager/role/roleSetupModule";
	}
	 // 添加 角色与模块关联关系操作
	@RequestMapping(value="/role_roleAddModule/{roleId}")
	public void roleAddModule(@PathVariable String roleId,HttpServletRequest request){
		String status = request.getParameter("status");
		map.put("moduleid", request.getParameter("moduleId"));
		map.put("roleid", roleId);
		if("true".equals(status)){
			if(!roleService.isRoleModule(map)){
				map.put("id",new Integer(SequenceManager.nextID("group_role"))+"");
				roleService.roleAddModule(map);
			};
		}else{
			if(roleService.isRoleModule(map)){
				//删除角色与模块（资源）关联关系
				roleService.delRoleModule(map);
			}
		}
	}
	//保存或更新角色
	@ResponseBody
	@RequestMapping(value="/role_saveOrUpdateRole", method=RequestMethod.POST)
	public  Map<String, Object> register(Role roleForm, HttpServletRequest request) {
		map.clear();
		if(roleForm.getId()!=null){// 修改角色
			if(roleService.update(roleForm)==1){
				map.put("msg", "角色修改成功!");
				map.put("success", true);
			}else{
				map.put("msg", "角色修改失败!");
				map.put("success", false);
			}
		}else{// 增加角色
			if(roleService.saveOrUpdateRole(roleForm)){
				map.put("msg", "角色增加成功!");
				map.put("success", true);
			}else{
				map.put("msg", "角色增加失败!");
				map.put("success", false);
			}
		}

		return map;
	}
	//删除角色信息 如果包含子记录则不能删除
	@ResponseBody
	@RequestMapping(value="/role_deleteRole/{roleId}")
	public Map<String, Object> enterShopList(@PathVariable String roleId, HttpServletRequest request) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
			if (roleService.deleteRole(roleId)) {
				jsonMap.put("success", true);
				jsonMap.put("msg", "删除角色成功！");
			} else {
				jsonMap.put("success", true);
				jsonMap.put("msg", "删除角色成功！");
			}
		
		return jsonMap;
	}
	//删除角色信息 如果包含子记录则不能删除
	@ResponseBody
	@RequestMapping(value="/role_getUserOfRole/{userId}")
	public Map<String, Object> getUserOfRole(@PathVariable String userId, HttpServletRequest request) {
		map.clear();
		map.put("userid", userId);
		List list = roleService.getUserOfRole(map);
		return PageModel.getListToString(list, request);
	}
	/*** 后台管理 END ***/
}